package thread;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-12-08
 * Time: 20:57
 */
public class T2 extends JFrame implements Runnable {
    JLabel lbl1 = new JLabel("现在的时间是:");
    //注释1:生成文本框对象
    JTextField txt = new JTextField(20);
    //注释2:生成窗口
    public T2() {
        super("多线程示例--每隔1秒刷新显示当前时间");
        setLayout(new FlowLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(lbl1);
        add(txt);
    }

    public void run() {
        //注释4:设置日期转换格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年MM月dd日HH:mm:ss,E");
        while (true)
        //注释5: 将转换得到的日期放到文本框中
            txt.setText(sdf.format(new Date()));
        //注释6:线程睡眠

    }

    public static void main(String[] args) {
        T2 target = new T2();
        target.setSize(400,200);
        target.setVisible(true);
        //注释7:显示窗口
        Thread thread = new Thread(target);
        //注释8:启动线程
        //thread.start();
    }
}


